﻿#include "blockgroup.h"
#include "block.h"
#include <QTimer>

BlockGroup::BlockGroup(const QPointF &point, QObject *parent)
    : QObject{parent}
{
    startPos = point;

//    colorTable << QColor(200,0,0,100) << QColor(255,200,0,100) << QColor(0,0,200,100) << QColor(0,200,0,100)
//               << QColor(0,200,255,100) << QColor(200,0,255,100) << QColor(150,100,100,100)
//               << QColor(Qt::darkGreen) << QColor(Qt::darkCyan) << QColor(Qt::darkBlue);

    colorTable << QColor(Qt::cyan) << QColor(Qt::yellow) << QColor(Qt::magenta) << QColor(Qt::green)
               << QColor(Qt::red) << QColor(Qt::blue) << QColor("#FF8800")
               << QColor(Qt::darkGreen) << QColor(Qt::darkCyan) << QColor(Qt::darkBlue);

    m_blockList.clear();
}

/*
 * @Brief:  在指定位置新建随机形状随机方向的方块组
 * @Param:  point 新方块的位置
 */
void BlockGroup::newShape()
{
    //首先读取游戏模式
    auto m_mode = (Mode)ST->settings()->value("GameMode", 0).toInt();
    deleteBlockFromGroup();

    //初始角度
    m_rotation = 90 * QRandomGenerator::global()->bounded(4);
    setRotation(m_rotation);
    //随机生成形状
    switch (m_mode) {
    case basic:
        m_blockshape = (BlockShape)(QRandomGenerator::global()->bounded(7));     //7种方块
        break;
    case variant:
        m_blockshape = (BlockShape)(QRandomGenerator::global()->bounded(11));    //11种方块
        break;
    default:
        break;
    }
    //重建形状
    blockListRemake();
    //重置方块组为起始位置
    setPos(startPos);
}


//void BlockGroup::copyShape(BlockGroup *another)
//{

//}

int BlockGroup::rotation() const
{
    return m_rotation;
}

BlockGroup::BlockShape BlockGroup::blockshape() const
{
    return m_blockshape;
}

/*
 * @Brief:  定义一个空的外轮廓
 * @Detail: 这里必须重新实现外轮廓函数，否则会使用第一个加入的子轮廓，
 *          碰撞检测出现问题
 */
QRectF BlockGroup::boundingRect() const
{
    return QRectF();
}

/*
 * @Brief:  重建方块组（包括重建形状）
 */
void BlockGroup::blockListRemake()
{
    //设置该方块组的颜色
    QColor boxColor = colorTable.at((int)m_blockshape);
    boxColor.setAlpha(100);
    //构造形状
    switch (m_blockshape) {
    case shape_I:                       // 竖线
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(2, 1);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(2, 3);
        m_blockList.at(3)->setCoord(2, 4);
        break;
    case shape_O:                       // O型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(1, 1);
        m_blockList.at(1)->setCoord(1, 2);
        m_blockList.at(2)->setCoord(2, 1);
        m_blockList.at(3)->setCoord(2, 2);
        break;
    case shape_T:                       // T型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(1, 2);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(3, 2);
        m_blockList.at(3)->setCoord(2, 1);
        break;
    case shape_S:                      // S型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(1, 2);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(2, 3);
        m_blockList.at(3)->setCoord(3, 3);
        break;
    case shape_Z:                       // Z型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(1, 2);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(2, 1);
        m_blockList.at(3)->setCoord(3, 1);
        break;
    case shape_J:                      // J型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(2, 1);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(2, 3);
        m_blockList.at(3)->setCoord(1, 3);
        break;
    case shape_L:                       // L型
        blockListCreate(4, boxColor);
        m_blockList.at(0)->setCoord(2, 1);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(2, 3);
        m_blockList.at(3)->setCoord(3, 3);
        break;
    case shape_T2:                      // 扩展T型，由5个方块组成
        blockListCreate(5, boxColor);
        m_blockList.at(0)->setCoord(1, 1);
        m_blockList.at(1)->setCoord(1, 2);
        m_blockList.at(2)->setCoord(1, 3);
        m_blockList.at(3)->setCoord(2, 2);
        m_blockList.at(4)->setCoord(3, 2);
        break;
    case shape_TR:                       // 凹形，由5个方块组成
        blockListCreate(5, boxColor);
        m_blockList.at(0)->setCoord(1, 1);
        m_blockList.at(1)->setCoord(1, 2);
        m_blockList.at(2)->setCoord(2, 2);
        m_blockList.at(3)->setCoord(3, 2);
        m_blockList.at(4)->setCoord(3, 1);
        break;
    case shape_I2:                       // 扩展竖线，由2个方块组成
        blockListCreate(2, boxColor);
        m_blockList.at(0)->setCoord(2, 2);
        m_blockList.at(1)->setCoord(3, 2);
    case shape_I3:                       // 扩展竖线，由3个方块组成
        blockListCreate(3, boxColor);
        m_blockList.at(0)->setCoord(1, 2);
        m_blockList.at(1)->setCoord(2, 2);
        m_blockList.at(2)->setCoord(3, 2);
        break;
    default:
        break;
    }
}

/*
 * @Brief:  重建方块组
 */
void BlockGroup::blockListCreate(int num, const QColor &brushColor)
{
//    qDebug()<<"当前方块数："<<m_blockList.count();
    for (int i = 0; i < num; ++i) {
        auto block = new Block(brushColor);
        m_blockList.append(block);
        addToGroup(block);
    }
}

/*
 * @Brief:  将小块从组内移走
 * @Detail: 移入场景中，并不删除，用于方块到底后操作
 */
void BlockGroup::moveBlockFromGroup()
{
    foreach (auto block, m_blockList) {
        removeFromGroup(block);
    }
    m_blockList.clear();
}

/*
 * @Brief:  将小块删除操作
 * @Detail: 用于复制操作
 */
void BlockGroup::deleteBlockFromGroup()
{
    foreach (auto block, m_blockList) {
        removeFromGroup(block);
        delete block;
    }
    m_blockList.clear();
}

/*
 * @Brief:  检测方块组内部是否发生碰撞
 */
bool BlockGroup::isCollding() const
{
    foreach (auto block, m_blockList) {
        //获取与该方块碰撞的方块的个数
        if(block->collidingItems().count() > 0) {
            return true;
        }
    }
    return false;
}
